#ifndef FLOORSERIAL_H
#define FLOORSERIAL_H

#include <QObject>
#include <QSerialPort>
#include <QByteArray>
#include <QThread>
#include <qserialportinfo.h>
#include <cstdint>
#include <QTimer>

class FloorSerial : public QObject {
    Q_OBJECT
    //Q_PROPERTY(float mainlight READ mainlight WRITE setmainlight NOTIFY mainlightChanged)
    //Q_PROPERTY(float leftlight READ leftlight WRITE setleftlight NOTIFY leftlightChanged)
    //Q_PROPERTY(float rightlight READ rightlight WRITE setrightlight NOTIFY rightlightChanged)
    //Q_PROPERTY(float fan1 READ fan1 WRITE setfan1 NOTIFY fan1Changed)
    //Q_PROPERTY(float fan2 READ fan2 WRITE setfan2 NOTIFY fan2Changed)


public:
    FloorSerial(QObject *parent = nullptr);
    ~FloorSerial();

    QStringList searchSerialPorts();

    void startVerifi();


    float mainlight() const { return m_mainlight; }
    void setmainlight(float mainlight);

    float leftlight() const { return m_leftlight; }
    void setleftlight(float leftlight);

    float rightlight() const { return m_rightlight; }
    void setrightlight(float rightlight);

    float fan1() const { return m_fan1; }
    void setfan1(float fan1);

    float fan2() const { return m_fan2; }
    void setfan2(float fan2);


signals:
    void unauthorized();
    void responce(const QString status,const QString detail);
    void closeSerialFinish();
    void openSerialFinish();
    void errorOccurred(const QString &errorString);
    void serialPortModelChanged();
    void readDate(const QByteArray &data);


    //配置
    void mainlightChanged();
    void leftlightChanged();
    void rightlightChanged();
    void fan1Changed();
    void fan2Changed();

public slots:

    void startVerifiSlot();
    void openPort(const QString &portName, const int baudRate);
    void closePort();
    void writeData(const QByteArray &data);
    void writeDataAddHead(const QByteArray &data);
    void readDateCallBack();
    void controlFan(int id, float speed);
    void controlLight(int id, float brightness);
    void replenishmentTrigge(float speed, int time);
    void holdserial();
    void holdsentTimer();
    //配置
    void valueTableLoad();

    void startMotor(float speed);
    void stopMotor();

private:
    QByteArray buffer_;
    QThread workerThread;
    QString portName_;
    int baudRate_;
    QStringList m_serialPortList;
    QSerialPort *serial_;

    uint8_t  Last_randomBytes[16];
    int unverifiedOvertime = 0;

    int isMotorcallback = false;
    bool isProcessing = false;  // 添加标志位
    //配置
    float m_mainlight;
    float m_leftlight;
    float m_rightlight;
    float m_fan1;
    float m_fan2;
    QTimer *verifiTimer = nullptr;
    QTimer *holdTimer = nullptr;

    QByteArray revMD5;

    int protocol_data_send(uint8_t function, uint8_t id, float value);
    int protocol_MD5_send(uint8_t *value);
    uint8_t check_sum(uint8_t init, uint8_t *ptr, uint8_t len);

    void initSetting();
    bool verifiMD5();
    void autoRetrybyCall(uint8_t function, uint8_t id, float value, int maxRetries = 3);
};

#endif // FLOORSERIAL_H
